/**
 * @class   PlayerPhonon
 * @brief   
 *
 *
 * @see     PlayerPhonon
 ****************************************************************************
 * @version $Id: PlayerPhonon.h 2887 2010-08-11 08:09:41Z alex $
 * @author  Alessandro Polo
 * @date    Released on 2009-10-19
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_DEVICES_PlayerPhonon_H__
 #define __WOSH_DEVICES_PlayerPhonon_H__

 #include <woshDefs.h>
 #include <core/LogContext.h>

 #include <framework/entertainment/MediaList.h>
 #include <interfaces/devices/PlayerAudio.h>

 #include <QThread>
 #include <Phonon/AudioOutput>
 #include <Phonon/SeekSlider>
 #include <Phonon/MediaObject>
 #include <Phonon/VolumeSlider>
 #include <Phonon/BackendCapabilities>

 #include "IPlayerPhononListener.h"


using namespace wosh;
using namespace wosh::entertainment;


class PlayerPhonon : public QThread
 {
	Q_OBJECT

	public:
		/**
		 * @brief   Default constructor. Init vars.
		 * @param log [in] reference to the logcontext to be used
		 */
		PlayerPhonon( LogContext& log );
		/**
		 * @brief   Deconstructor. If playing or idle (thread is stll running), it will be stopped.
		 */
		virtual ~PlayerPhonon();


	public:

		virtual WRESULT play( int index );

		virtual WRESULT play()				{ return play(""); }


		/**
		 * @brief   Play a resource
		 * @param stream_uri [in] the URI of the resource to be played. Multiple protocols are supported:
		 *                         - filesystem [file:///path/to/somefile.avi] (default: '/path/to/somefile.avi' will work too) 
		 *                           Also folders are supported, but it will clear the playlist and enqueue files, not recursive.
		 *                         - Network stream, HTTP [http://www.alessandropolo.name/music/sounday/Longe-at-the-Counter.mp3]
		 *                         - Compact Disc Digital Audio [cdda://4]
		 *                         - DVD [dvd://1]
		 */
		virtual WRESULT play( const std::string& stream_uri );

		virtual WRESULT enqueue( const std::string& stream_uri );

		virtual WRESULT remove( int index );

		virtual WRESULT pause();
		virtual WRESULT stop();

		virtual WRESULT first();
		virtual WRESULT next();
		virtual WRESULT prev();
		virtual WRESULT last();

		virtual WRESULT clear();

		virtual WRESULT setVolume( int newVolume );
		virtual WRESULT setVolumeOffset( int volume_span );

		virtual WRESULT setMute( bool on );

		virtual void setRepeatItem( bool value );
		virtual void setRepeatPlayList( bool value );

		virtual WRESULT setDeviceDriver( const std::string& dev_driver );
		virtual WRESULT setDeviceOutput( const std::string& dev_file );

		virtual WRESULT setPosition( float position_rate );
		virtual WRESULT setPositionOffset( float position_rate_offset );

		virtual WRESULT setPositionMs( unsigned long position_rate );

		virtual float getPosition();
		virtual std::string getPositionStr();
		virtual unsigned long getPositionMs();

		inline const std::string& getDeviceDriver() const		{ return this->device_sink; }
		inline const std::string& getDeviceOutput() const		{ return this->device_output; }

		virtual int getVolume();
		virtual bool isMuted();

		virtual bool isPlaying();


		inline int getPlayListIndex() const		{ return this->playlist.getCurrentIndex(); }

		inline MediaList& getPlayList()			{ return this->playlist; }


		inline void setPlayerListener( IPlayerPhononListener* Listener ) {
			this->playerListener = Listener;
		 }


		static std::string getPhonon2String( Phonon::State state );

/** @name GStreamer callbacks
 * @{
 ******************************************************************************/
	protected slots:

		void stateChanged(Phonon::State newState, Phonon::State oldState);
	//	void tick(qint64 time);
		void sourceChanged(const Phonon::MediaSource &source);
		void metaDataChanged();
		void metaStateChanged(Phonon::State newState, Phonon::State oldState);
		void aboutToFinish();

//@}
/** @name Thread methods
 * @{
 ******************************************************************************/
	protected:
		virtual void run();

//@}
/** @name Static methods
 * @{
 ******************************************************************************/
	public:

//@}
/** @name Internal methods
 * @{
 ******************************************************************************/
	protected:

		WRESULT play_( MediaTrack* item, bool synch = true );

		WRESULT play_index_( int index, bool synch = true );

		int enqueue_( const std::string& file_url );


//@}
/** @name Internal vars
 * @{
 ******************************************************************************/
	private:

		Phonon::SeekSlider* seekSlider;
		Phonon::MediaObject* mediaObject;
		Phonon::MediaObject* metaInformationResolver;
		Phonon::AudioOutput* audioOutput;
		Phonon::VolumeSlider* volumeSlider;

		std::string device_sink;		///< output driver GST name (default: alsasink)
		std::string device_output;		///< ALSA device name (default; ..)

		MediaList playlist;				///< the playlist

		short currentRepeat;			///< flag: repeat current item forever
		short playListRepeat;			///< flag: repeat whole playlist forever

		int volume_init;				///< (default) volume value (used only while starting thread)

		LogContext& Log;

		IPlayerPhononListener* playerListener;

//@}


}; // class def


#endif //__WOSH_DEVICES_PlayerPhonon_H__
